\documentclass[12pt]{article}
%\title{Package Test: algorithm/algorithmic}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{algorithm}
\usepackage{algorithmic}
\title{Algorithm Exercise 03}
\date{2018-12-08}
\author{Zhiqing Rui \\ ID:2018Z8020661080}
\begin{document}
\maketitle
\begin{algorithm}
\caption{DominoSum($A$)}

\begin{algorithmic}
 \REQUIRE \\
 A:Array of Cards\\
 $Card.L$ is the left value of Card\\
 $Card.R$ is the right value of Card\\
 $n$ is the length of $A$ \\
 \RETURN \\
 DominoSum: Sum of Domino Cards Array\\
 A: order of Domino Array
 \STATE $A \leftarrow [Card(0,0)],A,[Card(0,0)]$
 \STATE (DominoSum,StatusArray)=DominoSumRec(A,0,n+1)
 \FOR{$i = 1 \rightarrow n$}
 \STATE minI=MIN(A[i].R,A[i].L)
 \STATE maxI=MAX(A[i].R,A[i].R)
 \IF{StatusArray[i] = 0}
 \STATE A[i].R=minI
 \STATE A[i].L=maxI
 \ELSE
 \STATE A[i].R=maxI
 \STATE A[i].L=minI
 \ENDIF
 \ENDFOR
 \RETURN DominoSum,A[1...n]
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\caption{DominoSumRec(A,first,last)}
\begin{algorithmic}

\IF{$last-first=1$}
\RETURN ($A[first].R*A[last].L),[]$
\ELSE
\STATE $mid =\lfloor ( first + last ) \rfloor/ 2$
\STATE $SL0 , SAL0 = DominoSumRec( A , first , mid )$
\STATE $SR0 , SAR0 = DominoSumRec( A , mid , last)$
\STATE $S0 = SL0 + SR0 $
\STATE $SA0=SAL0+[CardStatus(A[mid])]+SAR0$
\STATE $Swap(A[mid].L,A[mid].R)$
\STATE $SL1 , SAL1 = DominoSumRec( A , first , mid )$
\STATE $SR1 , SAR1 = DominoSumRec( A , mid , last)$
\STATE $S1 = SL1 + SR1 $
\STATE $SA1=SAL1+[CardStatus(A[mid])]+SAR1$
\IF{$S0 \ge S1$}
\RETURN $(S0,SA0)$
\ELSE
\RETURN $(S1,SA1)$
\ENDIF
\ENDIF
\end{algorithmic}
\end{algorithm}

\begin{algorithm}
\caption{CardStatus(Card)}
\begin{algorithmic}
 \IF {$Card.L \leq Card.R$}
 \RETURN 0
 \ELSE
 \RETURN 1
 \ENDIF
\end{algorithmic}
\end{algorithm}


\end{document}